(function($)
{
    $.fn.loadCharts=function(params)
    {
    	params = $.extend({
    	url: "http://...", 
    	action : "GETLIST", 
    	caller : "SALESFORCES", 
    	type : "PRODUCT",
    	gType : "ORDER_HISTORY",
    	year : "2013",
    	chartType : "ColumnChart",
    	lang : "FR",
		/* @bferrazzini : Type de graphique possible, todo : TableChart
		PieChart, ScatterChart,	GaugeChart, GeoChart, TreeMapChart, ComboChart, LineChart, BarChart, ColumnChart, AreaChart, CandlestickChart, BubbleChart
		--> https://developers.google.com/chart/interactive/docs/gallery?hl=fr*/
    	nom : "none"}, params); // Merge des paramètres
      	params.url = params.url + "&action=" + params.action + "&caller=" + params.caller + "&nom=" + params.nom;
      	
      	return this.each(function()
       	{
       		var divId = this.id;
       		try{
		         cont = $.getJSON(params.url, function(result) {
		         cont = result.data;
		         
		         // Graphique pour produits -> Vente
		         if (params.type == "PRODUCT" && params.gType == "ORDER_HISTORY"){
	
					var options = {title: 	params.nom + ' ' + params.gType};
		         	var dataArray = [['years', params.year]];
		         	
						for (var x in cont.salesforces[0].orders_history){
							// REplace 0 par i -> @bferrazzini
							if (cont.salesforces[0].orders_history[x].year == params.year){				
								dataArray.push([ getMonth(params.lang, parseInt(cont.salesforces[0].orders_history[x].month)), parseInt(cont.salesforces[0].orders_history[x].value) ]);
							}
						}
						//Trie par Ordre Décroissant le tableau 
						/*dataArray.sort(function(a, b){
						    var a1= a[1], b1= b[1];
						    if(a1== b1) return 0;
						    return getMonth(params.lang, a1> b1? 1: -1);
						});*/
						
					 	var data = google.visualization.arrayToDataTable(dataArray);
		         }
		  	
		     	LoadCharts(params.chartType, data, options, divId);
				});
			
			}catch(err){
				_onError(err);
			}
        });                        
    };
})(jQuery);

 //Appel de la fonction google  paramètre ( type de graphique, donnée graphiques, options du graphiques)
function LoadCharts(chartType, data, options, div){
	if(chartType == "BarChart"){google.setOnLoadCallback(drawBarChart(data, options, div));}
	if(chartType == "PieChart"){google.setOnLoadCallback(drawPieChart(data, options, div));}
	if(chartType == "ColumnChart"){google.setOnLoadCallback(drawColumnChart(data, options, div));}
	if(chartType == "BubbleChart"){google.setOnLoadCallback(drawBubbleChart(data, options, div));}
	if(chartType == "CandlestickChart"){google.setOnLoadCallback(drawCandlestickChart(data, options, div));}
	if(chartType == "ComboChart"){google.setOnLoadCallback(drawComboChart(data, options, div));}
	if(chartType == "GaugeChart"){google.setOnLoadCallback(drawGaugeChart(data, options, div));}
	if(chartType == "GeoChart"){google.setOnLoadCallback(drawGeoChart(data, options, div));	}
	if(chartType == "LineChart"){google.setOnLoadCallback(drawLineChart(data, options, div));}
	if(chartType == "ScatterChart"){google.setOnLoadCallback(drawScatterChart(data, options, div));}
	if(chartType == "AreaChart"){google.setOnLoadCallback(drawSteppedAreaChart(data, options, div));}
	if(chartType == "TableChart"){google.setOnLoadCallback(drawTableChart(data, options, div));}
	if(chartType == "TreeMapChart"){google.setOnLoadCallback(drawTreeMapChart(data, options, div));}
}
	
//listes des fonction d'appel de Charts Google
function drawPieChart(data, options, div){
	var chart = new google.visualization.PieChart(document.getElementById(div));
	chart.draw(data, options);
}

function drawBarChart(data, options, div){
	var chart = new google.visualization.BarChart(document.getElementById(div));
	chart.draw(data, options);
}

function drawColumnChart(data, options, div){
	var chart = new google.visualization.ColumnChart(document.getElementById(div));
	chart.draw(data, options);
}

function drawAreaChart(data, options, div){
	var chart = new google.visualization.AreaChart(document.getElementById(div));
	chart.draw(data, options);
}

function drawBubbleChart(data, options, div){
	var chart = new google.visualization.BubbleChart(document.getElementById(div));
	chart.draw(data, options);
}

function drawCandlestickChart(data, options, div){  
	var chart = new google.visualization.CandlestickChart(document.getElementById(div));
	chart.draw(data, options);
}

function drawComboChart(data, options, div){
	var chart = new google.visualization.ComboChart(document.getElementById(div));	   
	chart.draw(data, options);
}

function drawGaugeChart(data, options, div){
	var chart = new google.visualization.Gauge(document.getElementById(div));	   
	chart.draw(data, options);
}

function drawGeoChart(data, options, div){
	var chart = new google.visualization.GeoChart(document.getElementById(div));	   
	chart.draw(data, options);
}

function drawLineChart(data, options, div){
	var chart = new google.visualization.LineChart(document.getElementById(div));	   
	chart.draw(data, options);
}

function drawScatterChart(data, options, div){
	var chart = new google.visualization.ScatterChart(document.getElementById(div));	   
	chart.draw(data, options);
}

function drawSteppedAreaChart(data, options, div){
	var chart = new google.visualization.SteppedAreaChart(document.getElementById(div));   
	chart.draw(data, options);
}

function drawTableChart(data, options, div){
	var table = new google.visualization.Table(document.getElementById(div));	   
	table.draw(data, options);
}

function drawTreeMapChart(data, options, div){
var tree = new google.visualization.TreeMap(document.getElementById(div));
tree.draw(data, {
          minColor: '#f00',
          midColor: '#ddd',
          maxColor: '#0d0',
          headerHeight: 15,
          fontColor: 'black',
          showScale: true});
}



//fonction qui retourne le mois en anglais ou Français selon les paramètres fournis.
function getMonth(lang, nb){
	if (lang == "FR"){
		switch (nb){
			case 1:
				return "Janvier";
			break;
			case 2:
				return "Février";
			break;
			case 3:
				return "Mars";
			break;
			case 4:
				return "Avril";
			break;
			case 5:
				return "Mai";
			break;
			case 6:
				return "Juin";
			break;
			case 7:
				return "Juillet";
			break;
			case 8:
				return "Aout";
			break;
			case 9:
				return "Septembre";
			break;
			case 10:
				return "Octobre";
			break;
			case 11:
				return "Novembre";
			break;
			case 12:
				return "Décembre";
			break;
			default:
				return "X";
			break;
		}
	}else if (lang == "EN"){
		return "X";
	}
	return "Z";
}

function _onError(err){
	alert (err);
}



//        var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
       // chart.draw(data, getOptions());
